define(function(require) {
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
	var fn = require("$UI/ls_common/fn");
	var Model = function() {
		this.callParent();
		this.TuihuoID = 0;
		this.total_upload_times = 0;
		this.finish_upload_times = 0;
	};
	Model.prototype.modelLoad = function(event) {
		console.log("modelLoad()");
	};
	Model.prototype.modelParamsReceive = function(event) {
		console.log("modelParamsRecive");
		console.log(event.params);
		this.TuihuoID = event.params.TuihuoID;
		this.TuihuoCode = event.params.TuihuoCode;
		this.PData_ID_List = event.params.PData_ID_List;
		this.TaskInit();
	};
	Model.prototype.TaskInit = function() {
		console.log("TaskInit");
		var self = this;
		this.comp("ID_LIST_Data").clear();
		console.log(this.PData_ID_List);
		this.PData_ID_List.each(function(e) {
			if (e.row.val("TuihuoID") === self.TuihuoID) {
				console.log(e.row.toJson('simple'));
				self.comp("ID_LIST_Data").add(e.row.toJson('simple'));
			}
		});
		console.log(this.comp("ID_LIST_Data").toJson());
		var data = {};
		data.username = window.app.username;
		data.password = window.app.password;
		data.ID = this.TuihuoID;
		this.comp("BarCode_Data").clear();
		var api_url = window.app.base_url + window.app.TuiHuoBarcodeApi;
		$.ajax({
			type : "POST",
			url : require.toUrl(api_url),
			data : {
				'data' : JSON.stringify(data)
			},
			dataType : 'json',
			success : function(res) {
				console.log(res);
				for (var i = 0; i < Object.keys(res).length; i++) {
					console.log(res[i]);
					self.comp("BarCode_Data").add({
						'code' : res[i].Pouchcode,
						'Type' : 1,
						'ID' : res[i].TuihuoInfoInfoID
					});
				}
				fn.cat_x5_data(self.comp("BarCode_Data"));
				return;
			},
			error : function(err) {
				console.log(err);
				return;
			}
		});
	};
	Model.prototype.SendButtonClick = function(event) {
		var self = this;
		self.total_upload_times = self.comp("ID_LIST_Data").count();
		self.finish_upload_times = 0;
		self.comp("ID_LIST_Data").each(function(param_P) {

			var data = {};
			data.TuiHuoID = param_P.row.val("TuihuoID");
			data.TuihuoCode = self.TuihuoCode;
			data.BarCode = [];
			self.comp("BarCode_Data").each(function(param) {
				var tmpdata = {
					ID : parseInt(param.row.val('ID') + ""),
					Type : parseInt(param.row.val('Type')),
					code : param.row.val('code')
				};
				if (tmpdata.ID == param_P.row.val("ID"))
					data.BarCode.push(tmpdata);
			});
			console.log(data.BarCode);
			console.log(data);
			var api_url = window.app.base_url + window.app.TuihuoAddApi;
			$.ajax({
				type : "POST",
				url : require.toUrl(api_url),
				data : {
					'data' : JSON.stringify(data)
				},
				dataType : 'json',
				success : function(res) {
					console.log(res[0]);
					if (res[0].errno != "0") {
						justep.Util.hint("数据上传出错，请联络管理员。");
						return;
					}
					self.finish_upload_times += 1;
					if (self.finish_upload_times == self.finish_upload_times) {
						self.getParent().comp("FactoryContents").to("TaskListContent");
					}
					return;
				},
				error : function(err) {
					console.log(err);
					return;
				}
			});
		});
	};
	Model.prototype.QRScan_ButtonClick = function(event) {
		console.log(event);
		var SubID = event.bindingContext.$object.val("ID");
		var IsBool = event.bindingContext.$object.val("IsBool");
		if (IsBool === undefined)
			IsBool = false;
		var prdcount = IsBool === false ? event.bindingContext.$object.val("PrdCount") : event.bindingContext.$object.val("BatchSum") - event.bindingContext.$object.val("PrdCount");
		this.comp("ScanCodeWindowContainer").load("Factory/Warehouse/ScanCode.w", {
			SubID : SubID,
			IsBool : IsBool,
			PrdCount : prdcount
		});
		this.comp("contents1").to("ScanCodeContent");
	};

	Model.prototype.BackBtnClick = function(event) {
		this.getParent().comp("FactoryContents").to("TaskListContent");
	};
	return Model;
});